package com.androider.timememory.db;

import java.util.ArrayList;
import java.util.HashMap;

import com.androider.timememory.utils.Constants;
import com.androider.timememory.utils.Dict;
import com.androider.timememory.utils.RecordUtil;
import com.androider.timememory.utils.StringUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts.Phones;

public class RecordModel {
    
	/**
	 * Insert a record to DB.
	 * @param context
	 * @param record
	 * @return
	 */
    public static boolean insertRecord(Context context, Record record) {
        RecordDAO dao = RecordDAO.getInstance(context);
        boolean isSuccessful = false;
        ContentValues value = new ContentValues();
        value.put(Dict.C_category, record.getCategory());
        value.put(Dict.C_type, record.getType());
        value.put(Dict.C_content, record.getContent());
        value.put(Dict.C_date, record.getDate());
        value.put(Dict.C_note, record.getNote());
        dao.open();
        isSuccessful = dao.insert(Dict.T_record, value); 
        dao.close();
        return isSuccessful;
    }
    
    /**
     * Get all records by returning a list.
     * @param context
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getRecordList(Context context) {
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        RecordDAO dao = RecordDAO.getInstance(context);
        dao.open();
        Cursor c = dao.query();
        while(c.moveToNext()) {
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put("id", c.getInt(c.getColumnIndex("id")));
        	map.put("category", RecordUtil.getCategory(c.getString(c.getColumnIndex("category"))));
        	map.put("type", RecordUtil.getType(context, c.getInt(c.getColumnIndex("type"))));
        	map.put("content", c.getString(c.getColumnIndex("content")));
        	map.put("date", StringUtil.getDate(c.getString(c.getColumnIndex("date"))));
        	map.put("note", c.getString(c.getColumnIndex("note")));
        	list.add(map);
        }
        dao.close();
        return list;
    }
    
    /**
     * Get a record by id.
     * @param context
     * @param id
     * @return
     */
    public static HashMap<String, Object> getRecordById(Context context, int id) {
    	HashMap<String, Object> map = new HashMap<String, Object>();
    	 RecordDAO dao = RecordDAO.getInstance(context);
         dao.open();
         Cursor c = dao.queryById(id);
         if(c.moveToFirst()) {
        	 map.put("id", c.getInt(c.getColumnIndex("id")));
             map.put("category", RecordUtil.getCategoryName(context, c.getString(c.getColumnIndex("category"))));
             map.put("type", c.getInt(c.getColumnIndex("type")));
             map.put("content", c.getString(c.getColumnIndex("content")));
             map.put("date", StringUtil.getDate(c.getString(c.getColumnIndex("date"))));
             map.put("note", c.getString(c.getColumnIndex("note")));
         }
         dao.close();
         return map;
    }
    
    /**
     * Get a record list by category.
     * @param context
     * @param category
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getRecordListByCategory(Context context, String category) {
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        RecordDAO dao = RecordDAO.getInstance(context);
        dao.open();
        Cursor c = dao.queryByCategory(category);
        if(c.moveToLast()) {
        	do {
        		HashMap<String, Object> map = new HashMap<String, Object>();
        		map.put("id", c.getInt(c.getColumnIndex("id")));
        		map.put("category", RecordUtil.getCategory(c.getString(c.getColumnIndex("category"))));
        		map.put("type", RecordUtil.getType(context, c.getInt(c.getColumnIndex("type"))));
        		map.put("content", c.getString(c.getColumnIndex("content")));
        		map.put("date", StringUtil.getDate(c.getString(c.getColumnIndex("date"))));
        		map.put("note", c.getString(c.getColumnIndex("note")));
        		list.add(map);
        	} while(c.moveToPrevious());
        }
        dao.close();
        return list;
    }
    
    /**
     * Get a record list by category and page number.
     * @param context
     * @param category
     * @param page
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getRecordListByCategory(Context context, String category, int page) {
    	ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    	RecordDAO dao = RecordDAO.getInstance(context);
    	final int itemCount = Constants.ITEM_COUNT_PER_PAGE;
    	final int start = (page - 1) * itemCount;
    	dao.open();
    	Cursor c = dao.queryByCategory(category);
    	c.moveToPosition(start - 1);
    	for(int i = 0; i < itemCount && c.moveToNext(); i++) {
    		HashMap<String, Object> map = new HashMap<String, Object>();
    		map.put("id", c.getInt(c.getColumnIndex("id")));
    		map.put("category", RecordUtil.getCategory(c.getString(c.getColumnIndex("category"))));
    		map.put("type", RecordUtil.getType(context, c.getInt(c.getColumnIndex("type"))));
    		map.put("content", c.getString(c.getColumnIndex("content")));
    		map.put("date", StringUtil.getDate(c.getString(c.getColumnIndex("date"))));
    		map.put("note", c.getString(c.getColumnIndex("note")));
    		list.add(map);
    	}
    	dao.close();
    	return list;
    }
    
    /**
     * Get the latest record by type.
     * @param context
     * @param type
     * @return
     */
    public static HashMap<String, Object> getLastImageRecord(Context context) {
        RecordDAO dao = RecordDAO.getInstance(context);
        dao.open();
        Cursor c = dao.queryImageRecord();
        if(c.moveToFirst()) {
        	HashMap<String, Object> lastMap = new HashMap<String, Object>();
    		lastMap.put("id", c.getInt(c.getColumnIndex("id")));
    		lastMap.put("category", RecordUtil.getCategory(c.getString(c.getColumnIndex("category"))));
    		lastMap.put("type", RecordUtil.getType(context, c.getInt(c.getColumnIndex("type"))));
    		lastMap.put("content", c.getString(c.getColumnIndex("content")));
    		lastMap.put("date", StringUtil.getDate(c.getString(c.getColumnIndex("date"))));
    		lastMap.put("note", c.getString(c.getColumnIndex("note")));
            dao.close();
            return lastMap;
        }else{
        	dao.close();
        	return null;
        }

    }
    
    /**
     * Search a record by the query string.
     * @param context
     * @param queryString
     * @return
     */
    public static ArrayList<HashMap<String, Object>> searchByNote(Context context, String queryString) {
    	ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    	RecordDAO dao = RecordDAO.getInstance(context);
    	dao.open();
    	Cursor c = dao.searchByNote(queryString);
    	if(c.moveToLast()) {
        	do {
        		HashMap<String, Object> map = new HashMap<String, Object>();
        		map.put("id", c.getInt(c.getColumnIndex("id")));
        		map.put("category", RecordUtil.getCategory(c.getString(c.getColumnIndex("category"))));
        		map.put("type", RecordUtil.getType(context, c.getInt(c.getColumnIndex("type"))));
        		map.put("content", c.getString(c.getColumnIndex("content")));
        		map.put("date", StringUtil.getDate(c.getString(c.getColumnIndex("date"))));
        		map.put("note", c.getString(c.getColumnIndex("note")));
        		list.add(map);
        	} while(c.moveToPrevious());
        }
    	dao.close();
    	return list;
    }
    
    /**
     * Get white list.
     * @param context
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getWhiteList(Context context) {
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        RecordDAO dao = RecordDAO.getInstance(context);
        dao.open();
        Cursor c = dao.queryWhiteList();
        while(c.moveToNext()) {
        	HashMap<String, Object> map = new HashMap<String, Object>();
//        	map.put("id", c.getInt(c.getColumnIndex("id")));
        	map.put("name", c.getString(c.getColumnIndex("name")));
        	map.put("number", c.getString(c.getColumnIndex("number")));
        	list.add(map);
        }
        dao.close();
        return list;
    }
    
    /**
     * Update white list.
     * @param context
     * @return
     */
    public static void updateWhiteList(Context context, ArrayList<HashMap<String, Object>> list) {
        RecordDAO dao = RecordDAO.getInstance(context);
        dao.open();
        dao.deleteAll(Dict.T_whitelist);
        for(int i = 0; i < list.size(); i++) {
        	ContentValues value = new ContentValues();
        	value.put(Dict.C_name, (String) list.get(i).get(Dict.C_name));
        	value.put(Dict.C_number, StringUtil.trimNumber((String) list.get(i).get(Dict.C_number)));
        	dao.insert(Dict.T_whitelist, value);
        }
        dao.close();
    }
    
    /**
     * Get the contacts cursor.
     * @param context
     * @return
     */
    public static Cursor getContactsCursor(Context context) {
    	RecordDAO dao = RecordDAO.getInstance(context);
    	return dao.queryContacts();
    }
    
    /**
     * Get the contacts list.
     * @param context
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getContacts(Context context) {
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        RecordDAO dao = RecordDAO.getInstance(context);
        dao.open();
        Cursor c = dao.queryContacts();
        while(c.moveToNext()) {
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put(Dict.C_name, c.getString(c.getColumnIndex(Phones.NAME)));
        	map.put(Dict.C_number, c.getString(c.getColumnIndex(Phones.NUMBER)));
        	list.add(map);
        }
        dao.close();
        return list;
    }
    
    /**
     * Get white list cursor.
     * @param context
     * @return
     */
    public static Cursor getWhiteListCursor(Context context) {
    	RecordDAO dao = RecordDAO.getInstance(context);
    	return dao.queryWhiteList();
    }
    
    /**
     * Update a data list.
     * @param preList
     * @param newList
     */
    public static void updateDataList(ArrayList<HashMap<String, Object>> preList, ArrayList<HashMap<String, Object>> newList) {
    	preList.removeAll(preList);
    	for(HashMap<String, Object> map : newList) {
			preList.add(map);
		}
    }
}
